package Longing;

 /*
     JDBC中的事务
     自动提交：只要执行任意一条DML语句，则自动提交一次。JDBC默认的事务行为
     但是在实际的业务中，需要N条DML语句联合才能完成。必须保证他们同时成功或者同时失败
     JDBC只要提交一次DML语句，就提交一次
     */


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class demo4 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement ps = null;
        String url = "jdbc:mysql://192.168.10.132:3306/learn_JDBC?useUnicode=true&characterEncoding=utf-8";
        String user = "root";
        String password = "123456";
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(url,user,password);

            String sql1 = "update user set user_name = ? where user_id = ? ";

            ps=connection.prepareStatement(sql1);

            ps.setString(1,"祝融");
            ps.setInt(2,20001);

            int count = ps.executeUpdate();
            System.out.println(count);

            ps.setString(1,"天工");
            ps.setInt(2,20002);

            count = ps.executeUpdate();
            System.out.println(count);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        finally {
            if (ps != null){
                try{
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null){
                try{
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    }

